package com.lck.blogmanager.controller;

import com.lck.blogmanager.common.Response;
import com.lck.blogmanager.exception.BusinessException;
import com.lck.blogmanager.req.AdminLoginReq;
import com.lck.blogmanager.req.UserLoginReq;
import com.lck.blogmanager.req.UserRegisterReq;
import com.lck.blogmanager.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户
 * @author wyq
 * @version v1.0.0
 * @explain
 * @date 2024/4/7 18:30
 */
@CrossOrigin
@RequestMapping("/user")
@RequiredArgsConstructor
@Slf4j
@RestController
public class UserController {

    private final UserService userService;

    /**
     * 用户登录
     * @param req
     * @return
     */
    @PostMapping("/login")
    public Response<Map<String,String>> userLogin(@RequestBody UserLoginReq req){
        Map<String,String> res = userService.userLogin(req);
        return Response.success(res);
    }

    /**
     * 用户注册
     * @param req
     * @return
     */
    @PostMapping("/register")
    public Response<Integer> userRegister(@RequestBody UserRegisterReq req){
        Integer id = userService.userRegister(req);
        return Response.success(id);
    }

    /**
     * 更新上传头像
     * @param file
     * @return
     */
    @PostMapping("/addPic")
    public Response<String> addPic(@RequestParam("file") MultipartFile file){
        String s = userService.addPic(file);
        return Response.success(s);
    }

    /**
     * 更新用户头像
     * @param file
     * @return
     */
    @PostMapping("/uploadPic")
    public Response uploadPic(@RequestParam("file") MultipartFile file){
        if(file==null){
            throw new BusinessException("头像为空，请后续重新上传");
        }
        userService.uploadPic(file);
        return Response.success();
    }

}
